package com.xqboss.apps.service.nft;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.ChainSeries;
import com.xqboss.apps.domain.nft.ChainTemplate;
import com.xqboss.apps.dto.nft.ChainSeriesDTO;
import com.xqboss.apps.dto.nft.ChainUnitDTO;
import com.xqboss.apps.mapper.nft.ChainSeriesMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class ChainSeriesService extends ServiceImpl<ChainSeriesMapper, ChainSeries> {

    @Autowired
    private ChainTemplateService chainTemplateService;

    public Long create(ChainSeriesDTO dto) {
        ChainSeries chainSeries = new ChainSeries();
        BeanUtils.copyProperties(dto, chainSeries);
        chainSeries.setSeriesLevels(JSON.toJSONString(dto.getLevelDTOS()));
        chainSeries.setSeriesUnitCounts(JSON.toJSONString(dto.getChainUnitDTOS()));
        chainSeries.setImg(dto.getTemplateImg());
        this.save(chainSeries);

        //todo 没有数据表 chainseriescategory
//        if (!dto.getSeriesCategory().isEmpty()){
//            chainSeriesCategoryService.insertCategories(dto.getSeriesCategory(),seriesId);
//        }
//        if (!dto.getSeriesAttrDTOS().isEmpty()) {
//            chainSeriesAttrService.add(dto.getSeriesAttrDTOS(), seriesId);
//        }
        return chainSeries.getId();
    }

    public ChainSeriesDTO simpleDetail(Long seriesId) {
        ChainSeries chainSeries = this.getById(seriesId);
        if (chainSeries == null) {
            return null;
        }
        ChainSeriesDTO chainSeriesDTO = new ChainSeriesDTO();
        BeanUtils.copyProperties(chainSeries, chainSeriesDTO);


        List<ChainUnitDTO> chainUnitDTOS = JSON.parseArray(chainSeries.getSeriesUnitCounts(), ChainUnitDTO.class);
        chainSeriesDTO.setChainUnitDTOS(chainUnitDTOS);

        ChainTemplate chainTemplate = chainTemplateService.getById(chainSeries.getTemplateId());
        chainSeriesDTO.setTemplateImg(chainTemplate.getImg());
        return chainSeriesDTO;
    }

    public void update(ChainSeriesDTO dto, Long seriesId) {
        ChainSeries chainSeries = this.getById(seriesId);
        if (chainSeries == null){
            return;
        }
        BeanUtils.copyProperties(dto,chainSeries);
//        chainSeries.setSeriesLevels(JSON.toJSONString(dto.getLevelDTOS()));
        chainSeries.setSeriesUnitCounts(JSON.toJSONString(dto.getChainUnitDTOS()));
        this.updateById(chainSeries);

//        chainSeriesCategoryService.insertCategories(dto.getSeriesCategory(),seriesId);

//        chainSeriesAttrService.add(dto.getSeriesAttrDTOS(),seriesId);
    }


}
